var _contextPath;

var _cardNum ;
var _rechargeMoney;
var _status;
var _type;
var _amount;
var _pwdresult;
var _pwd1;
var _pwd2;
var st = true;
var st1 = true;
var _pwd1result;
$(document).ready(function() {
	_contextPath = $("#contextPath").val();
	$("#accountpwd1").hide();
	$("#accountpwd2").hide();
});

function getContextPath() {
	if ("" == _contextPath) {
		_contextPath = $("#contextPath").val();
	}
	return _contextPath;
}

/**
 * 清除表单
 */
function clearForm() {
	$('#cardfrom').form('clear');
	$('#radio1').attr('checked',true);
}

function accountpwd() {
	var val = $('#rechargeMoney').val();
	if (val >= 1000) {
		$("#accountpwd1").show();
		$("#accountpwd2").show();
	}else{
		$("#accountpwd1").hide();
		$("#accountpwd2").hide();
	}
}
function numberCheck(pwd){
	var reg = new RegExp("^[0-9]*$");
    if(!reg.test(pwd)){
    	// alert('请填入正确数字!');
    	return false;
    }
    return true;
}

/**
 * 卡激活
 */
function activate() {
//	if (!$("#form").form('validate')){
//		return false;
//	}
	var cardNum = $('#cardNum').val();
	if (cardNum == '' || typeof (cardNum) == 'undefined') {
		cardNum.uppShow('未输入卡号!');
		return;
	}
	var rechargeType = $('input:radio:checked').val();
	var rechargeMoney = $('#rechargeMoney').val();
	var checkMoney = numberCheck(rechargeMoney);
	if(_status == 0){
		top.uppShow('此卡必须先出库才能被激活');
		return false;
	}
	if(!checkMoney){
		top.uppShow('请填入正确数字!');
		return false;
	}
	var pwd = $('#pwd').val();
	var pwd2 = $('#pwd2').val();
	var equal = isEquals(pwd, pwd2);
	if(!equal){
		top.uppShow('两次输入不一致!');
		return false;
	}
	var check = numberCheck(pwd);
	if(!check){
		top.uppShow('请填入正确数字!');
		return false;
	}
	$.ajax({
		type : 'POST',
		url : contextPath + 'iccard/rechargecard/activate',
		data : {
			'cardNum' : cardNum,
			'rechargeType' : rechargeType,
			'rechargeMoney' : rechargeMoney,
			'pwd' : pwd,
		},
		dataType : 'json',
		success : function(root) {
			if (root.opResult == '0') {
				top.uppShow(root.msgDesc);
				gotoNormallist();
			} else {
				top.uppShow(root.msgDesc);
			}
		},
		error : function(data) {
			uppAjaxError(data);
		}
	});
}
function checkPwd(){
	var pwd = $('#pwd').val();
	var pwd2 = $('#pwd2').val();
	var equal = isEquals(pwd, pwd2);
	if(equal){
		showSubmit();
	}else{
		removeSubmit();
	}
}
function isEquals(str1, str2) {
	if (str1 != str2) {
		top.uppShow('两次输入不一致!');
	}
	return true;
}

function gotoNormallist(){
	window.location.href = contextPath + 'iccard/recharge/rechargecard_searchNormal.jsp';
}

function showSubmit(){
	$('#confir').attr('href','#');
	$('#confir').attr('style','color:red');
	$('#confir').attr('onclick','javascript:activate();');
}
function removeSubmit(){
	$('#confir').attr('style','color:#BEBFC3');
	$('#confir').removeAttr('href');
}
$.extend($.fn.validatebox.defaults.rules, {
	positive_int : {
		validator : function(value, param) {
			if (value) {
				return /^[0-9]*[1-9][0-9]*$/.test(value);
			} else {
				return true;
			}
		},
		message : '只能输入正整数.'
	},
	recharmoney_int : {
		validator : function(value, param) {
			if (value) {
				return /^[0-9]*[1-9][0-9]*$/.test(value);
			} else {
				return true;
			}
		},
		message : '为此卡激活，交易金额必须是大于0的正整数.'
	},
	checkCarNum : {
        validator : function (value, param) {
            if(!/^[0-9]\d{18}$/.test(value,param)){
                $.fn.validatebox.defaults.rules.checkCarNum.message = '请输入19位卡号';
                return false;
            }else{
            	return true;
            }
        },
		message : '请输入19位卡号'
	},
	checkeRechargeMoney: {
        validator : function (value, param) {
        	if(!/^[0-9]*[1-9][0-9]*$/.test(value)){
                $.fn.validatebox.defaults.rules.checkeRechargeMoney.message = '交易金额必须是正整数.';
                return false;
            }
        	var cardNum = $('#cardNum').val();
        	 var data = {cardNum:cardNum};
        	 _rechargeMoney  = value;
        	 if(_cardNum != cardNum){
        		 st = true;
        	 }
        	 if(st){
        		 $.ajax({ 
        			 url : _contextPath+'/iccard/rechargecard/loadInfoByCardNum',
        			 data : data,
        			 async : false,
        			 type : "POST",
        			 success : function(root){
        				 var amount = root.amountTotal;
        				 // 0：新卡
        				 // 1：复用卡
        				 var type = root.type;
        				 /*
        				  * 0:入库 1:出库 2:激活 3:冻结 4:销户
        				  */
        				 var status = root.status;
        				 var pwdresult = root.pwdresult;
        				 _amount = amount;
        				 _type = type;
        				 _status = status;
        				 _pwdresult = pwdresult;
        				 //alert("cardNum:"+cardNum+",rechargeMoney:"+rechargeMoney+",status:"+status+",type:"+type+",amout:"+amount);
        			 }
        		 })
        		 st=false;
        		 _cardNum = cardNum;
        	 }
        	//新卡
        	 if(_type == 0){
        		 if(_rechargeMoney == 0){
        			 $("#accountpwd1").hide();
					 $("#accountpwd2").hide();
					 removeSubmit();
					 $.fn.validatebox.defaults.rules.checkeRechargeMoney.message = '为此卡激活，交易金额必须是大于0的正整数';
		             return false;
        		 }
        		 else if(_rechargeMoney > 1000){
					 $("#accountpwd1").show();
					 $("#accountpwd2").show();
					 return true;
				 }else{
					 $("#accountpwd1").hide();
					 $("#accountpwd2").hide();
					 showSubmit();
					 return true;
				 }
        	 }
            
        },
		message : '请输入充值金额'
	},
	checkePwd: {
        validator : function (value, param) {
        	if(!/^[0-9]\d{5}$/.test(value,param)){
                $.fn.validatebox.defaults.rules.checkePwd.message = '请输入6位纯数字作为密码';
                return false;
            }
        	var cardNum = $('#cardNum').val();
          	var data = {cardNum:cardNum,pwd:value};
          	 if(_pwd1 != value){
          		 st1 = true;
          	 }
          	 if(st1){
          		 $.ajax({ 
          			 url : _contextPath+'/iccard/rechargecard/loadInfoByCardNum',
          			 data : data,
          			 async : false,
          			 type : "POST",
          			 success : function(root){
          				 var amount = root.amountTotal;
          				 // 0：新卡
          				 // 1：复用卡
          				 var type = root.type;
          				 /*
          				  * 0:入库 1:出库 2:激活 3:冻结 4:销户
          				  */
          				 var status = root.status;
          				 var pwdresult = root.pwdresult;
          				var pwd1result = root.pwd1result;
          				 _amount = amount;
          				 _type = type;
          				 _status = status;
          				 _pwdresult = pwdresult;
          				 _pwd1result = pwd1result;
          				 //alert("cardNum:"+cardNum+",rechargeMoney:"+rechargeMoney+",status:"+status+",type:"+type+",amout:"+amount);
          			 }
          		 })
          		 st1 = false;
          		 //保存第一次密码
            	 _pwd1 = value;
        		if(_pwd1result == 0){
        			removeSubmit();
        			$.fn.validatebox.defaults.rules.checkePwd.message = '交易密码不能是系统默认密码';
        			return false;
        		}else{
        			return true;
        		}
          	 }
        	if(value == ''){
        		$.fn.validatebox.defaults.rules.checkePwd.message = '密码必须为6位纯数字.';
        		return false;
        	}
        	else if(_pwdresult == 0){
        		$.fn.validatebox.defaults.rules.checkePwd.message = '交易密码不能是系统默认密码';
    			return false;
        	}else {
       	 		return true;
       	 	}
        },
		message : '密码必须为6位纯数字.'
	},
	checkePwds : {
        validator : function (value, param) {
        	 _pwd2 = value;
        	 if(_pwd2 != _pwd1){
        		 $.fn.validatebox.defaults.rules.checkePwds.message = '两次密码不一致';
	             return false;
        	 }else{
        		 removeSubmit();
        		 showSubmit();
        		 return true;
        	 }
        },
		message : '密码必须为6位纯数字'
	}
});

